﻿package zf.media.audio.spectrum
{
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	
	import zf.media.audio.SoundSpectrumMode;

	/**
	 * 光束声谱图形 
	 * @author mike.zhang
	 * 
	 */
	public class RaySpectrum extends SoundSpectrum
	{
		private var lineSprite:Sprite;
		//private var bgData:BitmapData;

		private var th:Number;
		private var vmin:Number;
		private var cp:Point;
		private var vx:Number;
		private var vy:Number;
		private var zjd:Number;
		private var ix:int;
		private var iy:int;
		
		private var randomCt2:ColorTransform;

		/**
		 * 创建一个光束声谱图形 
		 * @param w 宽度
		 * @param h 高度
		 * 
		 */
		public function RaySpectrum (w:Number,h:Number):void
		{
			super (w,h);
			type=SoundSpectrumMode.RS;
			randomCt2=getCt();
			ff=16;
			cp=new Point(ww/2,hh/2);
			lineSprite=new Sprite;
			vmin=ww>hh?hh:ww;
			th=vmin*2.5;
			zjd=0;
			vx=creatV();
			vy=creatV();
			//cr=0;
			//cg=0;
			//cb=0;
			//lineColor=new ColorTransform(1, 1, 1, 1, cr, cg, cb);
			vx=Math.random()>0.5?vx:-vx;
			vy=Math.random()>0.5?vy:-vy;
			ix=creatF(vx);
			iy=creatF(vy);
		}
		/**
		 * @private 
		 * 
		 */
		override protected function draw ():void
		{
			super.draw ();
						
			lineSprite.graphics.clear ();
			lineSprite.graphics.lineStyle (1,randomCt2.color);
			for (var i:int=0; i<ff; i++)
			{
				var jd:Number=i*Math.PI*2/ff;
				lineSprite.graphics.moveTo (cp.x,cp.y);
				//trace(Math.cos(jd)+"---"+Math.sin(jd))
				lineSprite.graphics.lineTo (Math.cos(jd+zjd)*th*Math.abs(byArr[i])+cp.x,Math.sin(jd+zjd)*th*Math.abs(byArr[i])+cp.y);
			}
			lineSprite.filters=new Array(new GlowFilter(randomCt2.color,1,1,1,1));
			if (cp.x>ww)
			//if (cp.x>ww*0.8)
			{
				vx=-1;
				//ix=-1;
				vx=vx>0?creatV ():-1*creatV ();
				vy=vy>0?creatV ():-1*creatV ();
				ix=creatF(vx);
				//trace("dx:"+ix);
			}
			else if (cp.x<0)
			//else if (cp.x<ww*0.2)
			{
				vx=1;
				//ix=1;
				vx=vx>0?creatV ():-1*creatV ();
				vy=vy>0?creatV ():-1*creatV ();
				ix=creatF(vx);
				//trace("dx:"+ix);
			}
			if (cp.y>hh)
			//if (cp.y>hh*0.8)
			{
				vy=-1;
				//iy=-1;
				vx=vx>0?creatV ():-1*creatV ();
				vy=vy>0?creatV ():-1*creatV ();
				iy=creatF(vy);
				//trace("dy:"+iy);
			}
			else if (cp.y<0)
			//else if (cp.y<hh*0.2)
			{
				vy=1;
				//iy=1;
				vx=vx>0?creatV ():-1*creatV ();
				vy=vy>0?creatV ():-1*creatV ();
				iy=creatF(vy);
				//trace("dy:"+iy);
			}
			_bg.bitmapData.draw (lineSprite,null,null,_blendMode);
			_bg.bitmapData.applyFilter (_bg.bitmapData,_bg.bitmapData.rect,new Point(),new BlurFilter(2,2,1));
			//_bg.bitmapData.applyFilter (_bg.bitmapData,_bg.bitmapData.rect,new Point(),new GlowFilter(randomCt2.color,1,4,4,4));
			_bg.bitmapData.colorTransform (_bg.bitmapData.rect,new ColorTransform(1, 1, 1, 1, -5, -10, -10));
			_bg.bitmapData.scroll(ix,iy);
			
			//_bg.bitmapData.scroll(2,2);
			
			cp.x+=vx;
			cp.y+=vy;
			zjd+=0.02;

			//if (Math.abs(cb)>=crgb)
			//{
			//cbv=-cbv;
			//
			//if (Math.abs(cg)>=crgb)
			//{
			//cgv=-cgv;
			//
			//if (Math.abs(cr)>=crgb)
			//{
			//crv=-crv;
			//}
			//cr+=crv;
			//}
			//cg+=cgv;
			//}
			//cb+=cbv;
			//
			//lineColor.redOffset=cr;
			//lineColor.greenOffset=cg;
			//lineColor.blueOffset=cb;
		}
		private function creatV ():Number
		{
			return vmin * 0.003 * Math.random();
		}
		private function creatF(num:Number):int
		{
			return -Math.ceil(4*num/vmin/0.003);
		}
		
		override public function defaultConfig():void
		{
			abs=false;
			machining=false;
		}
	}
}